package action;

import global.VerifyCode;
import global.security.AuthenticatorHolder;
import global.security.ClientSession;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.UserService;
import util.ServletHelp;

/**
 * 用户登录登出Action类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class LoginAction extends BaseAction {
	/**
	 * 用户Service
	 * */
	private UserService userService = null;

	/**
	 * @param userService
	 *            the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 用户登录
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		// 用户ID
		String userId = request.getParameter("userId");
		// 密码
		String password = request.getParameter("password");
		
		Map loginMap = userService.login(userId, password);
		if((Boolean)loginMap.get("success")){
			// 设置Session
			request.getSession().setAttribute("CLIENT_SESSION", (ClientSession)loginMap.get("session"));
		}
		
		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", (Boolean)loginMap.get("success"));
		res.put("message", (String)loginMap.get("message"));
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 用户退出
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward logout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 清除Session
		request.getSession().removeAttribute("CLIENT_SESSION");
		AuthenticatorHolder.closeClientSession();
		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 用户注册
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward register(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = ServletHelp.getParameterMap(request);
		HttpSession session = request.getSession();
		map.put("verifyCodeInSession", (String) session.getAttribute("verifyCode"));
		map.put("request", request);

		// 用户注册
		JSONObject res = userService.register(map);
		
		// 返回结果
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 重置密码
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward resetPwd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = ServletHelp.getParameterMap(request);
		HttpSession session = request.getSession();
		map.put("verifyCodeInSession", (String) session.getAttribute("verifyCode"));
		
		// 重置密码
		JSONObject res = userService.resetPwd(map);
		
		// 返回结果
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 获取验证码
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward getVerifyCode(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 设置浏览器不缓存本页
		response.setHeader("Cache-Control", "no-cache");

		// 生成验证码，写入用户session
		String verifyCode = VerifyCode.generateTextCode(
				VerifyCode.TYPE_NUM_LOWER, 5, null);
		request.getSession().setAttribute("verifyCode", verifyCode);

		// 输出验证码给客户端
		response.setContentType("image/jpeg");
		BufferedImage bim = VerifyCode.generateImageCode(verifyCode, 60, 20, 3,
				true, Color.WHITE, Color.BLACK, null);
		ImageIO.write(bim, "JPEG", response.getOutputStream());

		return null;
	}
	

	/**
	 * 用户验证
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward loginCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 用户ID
		String userId = request.getParameter("userId");
		// 密码
		String password = request.getParameter("password");
		
		Map loginMap = userService.login(userId, password);
		if((Boolean)loginMap.get("success")){
			// 设置Session
			request.getSession().setAttribute("CLIENT_SESSION", (ClientSession)loginMap.get("session"));
		}
		
		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", (Boolean)loginMap.get("success"));
		res.put("message", (String)loginMap.get("message"));
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
}
